# -*- coding: utf-8 -*-

"""
  @author: tangliqi
  @date: 2022/10/25 18:02
  @python version: 3.6 
  
  ---------------------------------------
  @desc: python算法实现 => 装饰器.py
"""
from functools import wraps


# 函数装饰器：

# ============================  不带参数的函数装饰器  ============================
def decorator_1(func):
    def wrapper(*args, **kwargs):
        # do something
        return func(*args, **kwargs)
        # do something

    return wrapper


# ============================  带参数的函数装饰器  ============================
def decorator_2(*args, **kwargs):
    # 传给装饰器的参数
    print(args)
    print(kwargs)

    def wrapper(func):

        def inner(*args, **kwargs):
            # 传给 func 被装饰函数本身的参数
            print(args)
            print(kwargs)

            return func(*args, **kwargs)

        return inner

    return wrapper


# 类装饰器

# ============================  不带参数的类装饰器  ============================
class Decorator_3:
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        print(args)
        print(kwargs)
        return self.func(*args, **kwargs)


# ============================  带参数的类装饰器  ============================
class Decorator_4:
    def __init__(self, level=None):
        self.level = level

    def __call__(self, func):
        print(self.level)

        def wrapper(*args, **kwargs):
            print(args)
            print(kwargs)
            return func(*args, **kwargs)

        return wrapper


# ============================  通过 wraps 找回被装饰函数本身的属性  ============================
class Decorator_5:
    def __init__(self, level=None):
        self.level = level

    def __call__(self, func):
        print(self.level)

        @wraps(func)
        def wrapper(*args, **kwargs):
            print(args)
            print(kwargs)
            return func(*args, **kwargs)

        return wrapper


# 被装饰函数
@Decorator_5()
def test(*args, **kwargs):
    pass


if __name__ == "__main__":
    test(5, 6, a=7)
    print(test.__name__)
